//
//  HomeViewController.swift
//  QQ空间
//
//  Created by 叶竞泽 on 2017/5/8.
//  Copyright © 2017年 叶竞泽. All rights reserved.
//

import UIKit

class HomeViewController: BaseViewController {
    
    // MARK: - 属性
    var colors: [UIColor] {
        return [UIColor.red, UIColor.green, UIColor.blue, UIColor.brown, UIColor.cyan, UIColor.darkGray]
    }
    /// 边栏
    var dockView: DockView?
    /// 当前显示在最上层的控制器
    var currentSubViewController: UIViewController? {
        didSet {
            if oldValue != currentSubViewController {
                oldValue?.view.removeFromSuperview()
            }
        }
    }
    
    // MARK: - 生命周期
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.black
        setupDock()
        setupSubControllers()
        didClickMenuItem(withType: .feed, fromButton: 0) // 设置初始状态
        // 监听屏幕旋转
        NotificationCenter.default.addObserver(self, selector: #selector(changeOrientation), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
    }
    
    // MARK: - 方法
    
    /// 监听设备旋转
    @objc private func changeOrientation() {
        guard let dockView = dockView, let controller = currentSubViewController else { return }
        showControllerViewAndResetFrame(controller: controller, dockView: dockView)
    }
}

// MARK: - 设置界面
extension HomeViewController {
    
    /// 设置dock
    fileprivate func setupDock() {
        dockView = DockView(inView: view)
        dockView?.bottomMenuView?.delegate = self
        dockView?.centerMenuView?.delegate = self
        dockView?.topInfoView?.delegate = self
        view.addSubview(dockView!)
    }
    
    /// 初始化子控制器
    fileprivate func setupSubControllers() {
        guard let dockView = dockView, let centerView = dockView.centerMenuView else { return }
        for i in 0 ..< centerView.subButtons.count {
            if i == 0 {
                let vc = AllStatusViewController()
                let nav = UINavigationController(rootViewController: vc)
                addChildViewController(nav)
                continue
            }
            let vc = UIViewController()
            vc.view.backgroundColor = colors[i]
            let nav = UINavigationController(rootViewController: vc)
            addChildViewController(nav)
        }
    }
}

// MARK: - dockView底部工具栏点击代理
extension HomeViewController: BottomMenuViewDelegate {
    /// dockView中的bottomView按钮点击回调
    ///
    /// - Parameter type: 按钮类型
    func didClickMenuItem(withType type: BottomMenuViewItemType) {
        let moodVC = MoodViewController()
        let nav = UINavigationController(rootViewController: moodVC)
        nav.modalPresentationStyle = .pageSheet
        nav.modalTransitionStyle = .coverVertical
        present(nav, animated: true) {
        }
    }
}

// MARK: - dockView中间工具栏点击代理
extension HomeViewController: CenterMenuViewDelegate {
    
    /// 显示控制器视图并设置视图的frame
    ///
    /// - Parameters:
    ///   - controller: 将要显示的控制器
    ///   - dockView: 当前显示的dockView
    fileprivate func showControllerViewAndResetFrame(controller: UIViewController, dockView: UIView) {
        let statusBarHeight = UIApplication.shared.statusBarFrame.height
        controller.view.frame = CGRect(x: dockView.bounds.width, y: statusBarHeight, width: UIScreen.main.bounds.width - dockView.bounds.width, height: UIScreen.main.bounds.height - statusBarHeight)
        view.addSubview(controller.view)
        currentSubViewController = controller
    }
    
    /// dockView中的dockView按钮点击回调
    ///
    /// - Parameter type: 按钮类型
    func didClickMenuItem(withType type: CenterMenuViewItemType, fromButton withTag: Int) {
        guard let dockView = dockView, let centerView = dockView.centerMenuView else { return }
        if childViewControllers.count != 0 && childViewControllers.count == centerView.subButtons.count {
            /**
            switch type {
            case .feed:
                showControllerViewAndResetFrame(controller: childViewControllers[withTag], dockView: dockView)
            case .passive:
                showControllerViewAndResetFrame(controller: childViewControllers[withTag], dockView: dockView)
            case .pic:
                showControllerViewAndResetFrame(controller: childViewControllers[withTag], dockView: dockView)
            case .album:
                showControllerViewAndResetFrame(controller: childViewControllers[withTag], dockView: dockView)
            case .friend:
                showControllerViewAndResetFrame(controller: childViewControllers[withTag], dockView: dockView)
            case .more:
                showControllerViewAndResetFrame(controller: childViewControllers[withTag], dockView: dockView)
            }
            */
            showControllerViewAndResetFrame(controller: childViewControllers[withTag], dockView: dockView)
        }
    }
}

// MARK: - topView顶部工具栏点击代理
extension HomeViewController: TopMenuViewDelegate {
    
    func didClickMenuItem(fromButton withTag: UIButton) {
        print(withTag)
    }
}
